<?php

namespace App\Listeners\V1;

use App\Events\V1\ForgotPasswordEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Zhuzhichao\IpLocationZh\Ip;

class ForgotPasswordLogListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ForgotPasswordEvent  $event
     * @return void
     */
    public function handle(ForgotPasswordEvent $event)
    {
        // 从事件中获取相关数据
        $objUser = $event->user;
        $objAgent = $event->agent;
        $arrReq = $event->req;
        $arrRsp = $event->rsp;

        // 日志描述
        $logDesc = 'event_forget_password_success';

        // 日志记录格式
        $logData['account'] = $objUser;
        $arrReq['reqIPAddress'] = IP::find($arrReq['reqIp']);
        $logData['req'] = $arrReq;
        $logData['rsp'] = $arrRsp;
        $agentData['device'] = $objAgent->device();
        $agentData['browser'] = $objAgent->browser() . ' ' . $objAgent->version($objAgent->browser());
        $agentData['platform'] = $objAgent->platform() . ' ' . $objAgent->version($objAgent->platform());
        $agentData['language'] = implode(' ', $objAgent->languages());
        $logData['agent'] = $agentData;
        $logData['time'] = time();

        $this->handleLog($logDesc, $logData);
    }

    /**
     * 写入日志函数
     *
     * @param 日志描述 $description
     * @param 日志内容 $data
     */
    protected function handleLog($description, $data)
    {
//        Log::info($description, $data);
    }
}
